package com.example.jiagoujiexiang.Controller;

import com.example.jiagoujiexiang.Utils.Constant;
import com.example.jiagoujiexiang.entity.Comment;
import com.example.jiagoujiexiang.entity.Pet;
import com.example.jiagoujiexiang.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.example.jiagoujiexiang.Service.petService;
import com.example.jiagoujiexiang.Service.commentService;
import com.example.jiagoujiexiang.Service.userService;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class IndexController {
    @Autowired
    petService petService ;
    @Autowired
    commentService commentService ;
    @Autowired
    userService userService ;
    //主页
    @GetMapping({"/", "/index"})
    public String index(HttpSession session, Model model) {
        addUserToModel(session,model);
        model.addAttribute("index_petsList", petService.getpetList());
        return "index";
    }

    //单狗狗详细页
    @GetMapping("/dogsDetails")
    public String dogsDetails(@RequestParam(name = "petdetail")int queryNumber,HttpSession session, Model model) {
        addUserToModel(session,model);
        model.addAttribute("dogdetails",petService.getdogsde(queryNumber));
        model.addAttribute("commentList",commentService.getCommentList());
        return "dogDetails";
    }
    //单猫咪详细页
    @GetMapping("/catsDetails")
    public String catsDetails(@RequestParam(name = "petdetail") int queryNumber,HttpSession session,  Model model) {
        addUserToModel(session,model);
        model.addAttribute("catdetails", petService.getcatsde(queryNumber));
        List<Comment> commentList = commentService.getCommentList();
        model.addAttribute("commentList", commentList);
        model.addAttribute("petdetail", queryNumber);
        return "catsDetails";
    }
    @PostMapping("/insertComment")
    public String insertComment(@RequestParam String name, @RequestParam String comment,HttpSession session,  Model model, @RequestParam int petdetail) {
        addUserToModel(session,model);
        int result = commentService.insertComment(name, comment);
        if (result > 0) {
            // 评论插入成功，重定向回原页面并保留 petdetail 参数
            return "redirect:/catsDetails?petdetail=" + petdetail;
        } else {
            // 评论插入失败，返回错误页面或提示信息
            return "error";
        }
    }
    @PostMapping("/insertReply")
    public String insertReply(@RequestParam int parentId, @RequestParam String name, @RequestParam String comment,HttpSession session,  Model model, @RequestParam int petdetail) {
        addUserToModel(session,model);
        int result = commentService.insertReply(parentId, name, comment);
        if (result > 0) {
            // 回复插入成功，重定向回原页面并保留 petdetail 参数
            return "redirect:/catsDetails?petdetail=" + petdetail;
        } else {
            // 回复插入失败，返回错误页面或提示信息
            return "error";
        }
    }


    //宠物搜索-狗狗
    @GetMapping("/moredogs")
    public String moredogs(@RequestParam(name = "dogspage",defaultValue = "1")int dogspage,HttpSession session,  Model model) {
        addUserToModel(session,model);
        List<Pet> petList=petService.getmoredogspage(dogspage);
        int countdogs = petService.getmoredogs().size();
        int loopCount = (int) Math.ceil((double) countdogs / 8);
        model.addAttribute("moredogs",petList);
        model.addAttribute("countdogs",petService.getmoredogs().size());
        model.addAttribute("countdogspage", dogspage);
        model.addAttribute("loopCount", loopCount);
        return "dogs";
    }
    //宠物搜索-猫猫
    @GetMapping("/morecats")
    public String morecats(@RequestParam(name = "catsspage",defaultValue = "1")int catspage,HttpSession session,  Model model) {
        addUserToModel(session,model);
        List<Pet> petList=petService.getmorecatspage(catspage);
        int countcats = petService.getmorecats().size();
        int loopCount = (int) Math.ceil((double) countcats / 8);
        model.addAttribute("morecats",petList);
        model.addAttribute("countcats",petService.getmorecats().size());
        model.addAttribute("countcatspage",catspage);
        model.addAttribute("loopCount", loopCount);
        return "cats";
    }
    //狗狗的饲养方法
    @GetMapping("/Havedogs")
    public String Havedogs(HttpSession session,  Model model) {
        addUserToModel(session,model);
        return "Havedogs";
    }
    private void addUserToModel(HttpSession session, Model model) {
        User user = userService.getUserByName((String) session.getAttribute(Constant.LOGIN_USER));
        if (user != null) {
            model.addAttribute("username", user.getUsername());
        }
    }
    //猫咪的饲养方法
    @GetMapping("/Havecats")
    public String Havecats(HttpSession session,  Model model) {
        addUserToModel(session,model);
        return  "Havecats";
    }
}
